<%--
  Created by IntelliJ IDEA.
  User: Jason
  Date: 2018/5/25
  Time: 9:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>计时</title>
</head>
<body>

<script>
    function startTime() {
        setTimeout(function () {
            alert("已用2小时");
        }, 7200000);
        time_start();
    }

    function time_start() {
        alert("开始计时");
        var dNOW = new Date();
        var t = dNOW.toLocaleTimeString();
        document.getElementById("date").innerHTML = "开始时间：" + t;
        var startTime = document.getElementById("date").value;
        ajaxCall("POST", "TimeServlet?method=insertStartTime&startTime=" + stratTime, checkResult);
    }

    function checkResult(res) {
        var obj = JSON.parse(res);
        if (obj) {
            alert("添加成功！");
        } else {
            alert("添加失败！");
        }
    }

        function stopTime() {
            alert("结束时间");
            var dStop = new Date();
            var t = dStop.toLocaleTimeString();
            document.getElementById("date1").innerHTML = "结束时间：" + t;
            var stopTime =document.getElementById("date1").value;
            ajaxCall("POST","TimeServlet?method=StopTime?stopTime="+stopTime,getResult);
        }

        function getResult(result){
            var obj = JSON.parse(result);
            if(obj){
               alert("题目做完了");
            }else{
                alert("失败!");
            }
        }


        //function time_now() {
        // alert("开始计时");
        // var dNow = new Date();
        //document.getElementById("date").innerHTML = "开始时间:" + two_char(dNow.getHours()) + ":" + two_char(dNow.getMinutes()) + ":" + two_char(dNow.getSeconds());
        //}

        function two_char(n) {
            return n >= 10 ? n : "0" + n;
        }

        function ajaxCall(method, url, callBackFunction) {
            var httprequest = new XMLHttpRequest();
            httprequest.onreadystatechange = function () {
                if (httprequest.readyState == 4 && httprequest.status == 200) {
                    callBackFunction(httprequest.responseText);
                }
            }
            httprequest.open(method, url, false);
            httprequest.send();
        }

</script>
<script src="../js/change.js"></script>
<p>点击“开始”按钮，两个小时后弹出提示信息。</p>
<button onclick="startTime()">开始</button>
<p id="date">开始时间:00:00:00</p>

<button onclick="stopTime()">停止计时</button>

</body>
</html>
